package com.first.xiinn.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import com.first.core.web.action.BaseAction;
import com.first.xiinn.model.BookCatalog;
import com.first.xiinn.model.BookManager;
import com.first.xiinn.service.BookCatalogService;
import com.google.gson.Gson;

public class BookCatalogAction extends BaseAction {
	@Resource
	private BookCatalogService catalogService;
	private BookCatalog catalog;

	public BookCatalog getCatalog() {
		return catalog;
	}

	public void setCatalog(BookCatalog catalog) {
		this.catalog = catalog;
	}

	private String bookid;

	public String getBookid() {
		return bookid;
	}

	public void setBookid(String bookid) {
		this.bookid = bookid;
	}
	public static String getUserMenuDoc() {
		return USER_MENU_DOC;
	}

	private static final String USER_MENU_DOC = "_USER_MENU_DOC";
	/**
	 * 添加
	 */
	public String save() {
		if("".equals(catalog.getCatalogId())){
			catalog.setCatalogId(null);
		}
		this.catalogService.save(catalog);
	    return "success";
	}
	/**
	 * 删除
	 */
	public String delete() {
		BookCatalog catalog = (BookCatalog)this.catalogService.get(this.bookid);
		this.catalogService.remove(catalog);
	    return "success";
	}
	/**
	 * 根据ID 查询章节
	 * @return
	 */
	public String search() {
		List list = this.catalogService.search(bookid);
		StringBuffer str = new StringBuffer("[");
		if (list != null) {
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				BookCatalog object = (BookCatalog) iterator.next();
				str.append('{').append('"').append("id").append('"')
						.append(':').append('"').append(object.getCatalogId())
						.append('"').append(',');
				str.append('"').append("text").append('"').append(':')
						.append('"').append(object.getCatalog()).append('"')
						.append(",").append('"').append("leaf").append('"')
						.append(':').append(true).append('}').append(',');
			}
		}
		String strs = str.toString().substring(0, str.toString().length() - 1);
		strs += "]";
		setJsonString(strs);
		return "success";
	}
	/**
	 * 获取单一章节信息
	 * @return
	 */
	  public String get(){
		  BookCatalog catalog = (BookCatalog)this.catalogService.get(this.bookid);
		  if(catalog==null){
			  catalog = new BookCatalog();
		  }
	    Gson gson = new Gson();
	    StringBuffer sb = new StringBuffer("{success:true,data:");
	    sb.append(gson.toJson(catalog));
	    sb.append("}");
	    setJsonString(sb.toString());

	    return "success";
	  }
	  
}
